package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.repository.StrategyCommentRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {
    @Autowired
    private StrategyCommentRepository strategyCommentRepository;

    @Autowired
    private MongoTemplate mongoTemplate;//可以操作数据库

    @Autowired
    private IDestinationService destinationService;//目的地服务类

    @Autowired
    private IUserInfoService userInfoService;



    @Override
    public StrategyComment get(String id) {
        Optional byId = strategyCommentRepository.findById(id);
//强转
        return (StrategyComment) byId.orElse(null);//如果是空是就返回空值,否则返回对象
    }

    @Override
    public void save(StrategyComment strategyComment) {

        Optional byId = strategyCommentRepository.findById(strategyComment.getId());
        StrategyComment comment = (StrategyComment)byId.orElse(null);
        strategyComment.setId(null);
        strategyCommentRepository.save(comment);
        String userId = strategyComment.getUserId();
        UserInfo userInfo = userInfoService.get(userId);

    }

//添加评论操作
//StrategyComment strategyComment  评论对象
    public void addComment(StrategyComment strategyComment,UserInfo userInfo) {

        //评论发表时间是提交的那一刻时间,不用用户填写的
        strategyComment.setCreateTime(new Date());
        //表示把(源对象userInfo的属性值设置给目标对象strategyComment的同名属性变量)
        //目的是当前登录用户添加的评论.所以评论对象的用户名称和用户等级等等和当前登录对象的是一样的()
        BeanUtils.copyProperties(userInfo,strategyComment);
        //因为用户对象id和评论对象的uerId不一样,要单独设置
        //是用户对象评论的,所以评论对象的userId和用户对象的id是一样的

    // 使用了  BeanUtils.copyProperties必须放在设置id为null之前
    // 新增方法必须设置id为空后,不设置id有值,才能执行新增操作,不然id有值了就执行更新操作
        strategyComment.setId(null);
        strategyComment.setUserId(userInfo.getId());
        //添加到数据库
        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public void thumbupnum(String cid, String uid) {
//  1.根据评论对象id,获取评论对象,再获取这个评论对象下的所有的评论
// 表示这个攻略下的评论
        StrategyComment comment = this.get(cid);
        List<String> thumbuplist = comment.getThumbuplist();
        if(!thumbuplist.contains(uid)){
            //不在,是点赞操作
            thumbuplist.add(uid);
            //评论对象点赞数在原来的基础上+1后,保存更新后的评论对象在数据库

            comment.setThumbupnum(comment.getThumbupnum()+1);
            this.save(comment);//有id是修改操作


        }else{
            //在,点赞过了,是取消点赞操作
                thumbuplist.remove(uid);
              comment.setThumbupnum(comment.getThumbupnum()-1);//-1
            this.save(comment);

        }

    }

    @Override
    public List<StrategyComment> listAll() {
        List<StrategyComment> strategyComments = strategyCommentRepository.findAll();
        return strategyComments;
    }




    @Override
    public Page<StrategyComment> query(StrategyCommentQuery qo) {
        //查询对象
        Query query = new Query();
        //根据id过滤出这个攻略下的所有的评论
        if(StringUtils.hasLength(qo.getStrategyId())) {
            Criteria criteria = Criteria.where("strategyId").is(qo.getStrategyId());
            query.addCriteria(criteria);
        }
        //查询出总数条数
        long rows = mongoTemplate.count(query, StrategyComment.class);
        if (rows == 0) {
            return  Page.empty();//如果总数条数是0,就返回空集合
        }
        //分页对象(设置当前页从0开始算,还有页面显示条数,按sequence这个列正序排列 )  
        //  Sort.Direction.ASC,表示按正序排列表格
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1,
                qo.getPageSize(), Sort.Direction.DESC, "createTime");
        //给查询对象
        query.with(pageable);


        List<StrategyComment> list = mongoTemplate.find(query, StrategyComment.class);
        PageImpl<StrategyComment> page = new PageImpl<>(list, pageable, rows);


        return page ;//相当于pageResult对象

    }



}
